<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Model {

    var $user_name   = '';
    var $password = '';    

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    function getAllUsers()
    {      
        //$this->db->order_by('first_name','ASC');  
        $query = $this->db->get('users');
        return $query->result();
    }
    function getUsers($row){
        
    }

    function getWorkPlaceForSector($sector_id){
        //select distinct user_radnomesto from users where user_radnomesto!=null;
        $this->db->distinct();
        $this->db->select('user_radnomesto');
        $this->db->where('sector_id', $sector_id);
        $this->db->where('user_radnomesto IS NOT NULL',null, false);  
        $this->db->where('user_radnomesto !=','');      
        $query=$this->db->get('users');
        //print_r($query->result());
        return  $query->result();
    }

    function getUsersData($row){
        //$this->db->order_by('users.id','ASC');
        $this->db->select('users.id,users.id,users.first_name,users.last_name,users.user_level,companies.company_name,sectors.sector_name,questionare.user_id,questionare.status,questionare.upitnik_prihvata,questionare.upitnik_odobrava');      
        if($this->user[0]->user_level==2){
            $this->db->where('user_level !=', 3);
            $this->db->where('users.sector_id', $this->user[0]->sector_id);           
        }
        $this->db->join('companies', 'users.company_id = companies.id', 'left');
        $this->db->join('sectors', 'users.sector_id = sectors.id', 'left');
        $this->db->join('questionare', 'users.id = questionare.user_id', 'left');
        $this->db->order_by("companies.company_name", "asc"); 
        $this->db->order_by("sectors.sector_name", "asc"); 
        $this->db->order_by("users.first_name", "asc"); 
        $this->db->order_by("users.last_name", "asc");
        $query=$this->db->get('users',10,$row);
        if($query->num_rows()>0){            
            return $query->result();            
        }
    }

    function getUser($user_id){
        $this->db->select('users.*,companies.company_name,sectors.sector_name,questionare.user_id,questionare.upitnik_prihvata,questionare.upitnik_odobrava');        
        $this->db->join('companies', 'users.company_id = companies.id', 'left');
        $this->db->join('sectors', 'users.sector_id = sectors.id', 'left');
        $this->db->join('questionare', 'users.id = questionare.user_id', 'left');
        $this->db->where('users.id',$user_id);        
        $this->db->limit(1);
        $this->db->from('users');        
        $query = $this->db->get();
        return  $query->row();
    }

    function getNumUsers(){
        if($this->user[0]->user_level==2){
            $this->db->where('user_level !=', 3); 
            $this->db->where('users.sector_id', $this->user[0]->sector_id);             
        }
        $query=$this->db->get('users');
        return count($query->result());        
    }
    function insertUser()
    {
        $this->user_name   = $this->input->post('user_name');
        $this->password = $this->input->post('password'); 
        //Ovo sam dodao, ali mislim i da je bilo u nekim prethodnim verzijama
        $this->first_name   = $this->input->post('first_name');
        $this->last_name   = $this->input->post('last_name'); 
        $this->user_level   = $this->input->post('user_level');   
        $this->company_id   = $this->input->post('company_id'); 
        $this->sector_id   = $this->input->post('sector_id');        
        $this->db->insert('users', $this);
    }
    

    function findUserByLogin()
    {
        $this->user_name   = $this->input->post('user_name');
        $this->password = $this->input->post('password');   
        //$this->db->select('id, user_name,first_name,last_name,user_level,user_firma,user_sektor');
        $this->db->from('users');
        $this->db->where(array('user_name'=>$this->user_name,'password'=> $this->password));        
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->result();
    }
    //Dodata izmena korisnika
    function updateUser($id)
    {
        $update_data = array(
                'user_name' => $this->input->post('user_name'),
                'first_name' => $this->input->post('first_name'),
                'last_name' => $this->input->post('last_name'),
                'user_level' => $this->input->post('user_level'),
                'company_id' => $this->input->post('company_id'),
                'sector_id' => $this->input->post('sector_id'),
                //'password' => md5($this->input->post('password '))
                'password' => $this->input->post('password')
            );
            $this->db->where('id',$id);
            $update = $this->db->update('users', $update_data);
            //$this->session->userdata('user');
    }

    function userCheck($auth_level){

    }
    // Dodato brisanje korisnika 
    function removeUser($user_id)
    {
        
        $this->db->where('id',$user_id);
        $this->db->delete('users');
    }



}

?>